/*
 * buzzer.c
 *
 * Created: 26/03/2012 11:26:43
 *  Author: Dani
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include "buzzer.h"

void BuzzerInit()
{
	// Buzzer pin as output.
	BUZZER_DDR |= BUZZER;
	// Timer 2: CTC mode, prescaler = 1024.
	TCCR2 |= 0b00001111;
	// Freq = 8M / (2 * 1024 * (1 + 9)) = 390 Hz
	OCR2 = 9;
}

void BuzzerEnable(unsigned char enable)
{
	if (enable)		
	{
		// Toogle OC2 on compare match
		TCCR2 |= 0b00010000;
	}	
	else
	{
		// OC2 disconected
		 TCCR2 &= ~0b00010000;
	}
		
}
/*
ISR (TIMER2_OVF_vect)
{
	// Toggle buzzer pin output.
	if (BUZZER_PIN & BUZZER)
		BUZZER_PORT &= ~BUZZER;
	else
		BUZZER_PORT |= BUZZER;
		
	TCNT2 = 256 - 10;
}*/

